<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索框 -->
      <el-input
        placeholder="请输入内容"
        v-model.trim="value"
        class="input-with-select"
      >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="btnFn"
        ></el-button>
      </el-input>
      <!-- 订单列表 -->
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column type="index" :index="tableData.user_id">
        </el-table-column>
        <el-table-column prop="order_number" label="订单编号" width="180">
        </el-table-column>
        <el-table-column prop="order_price" label="订单价格"> </el-table-column>
        <el-table-column prop="pay_status" label="是否付款">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.order_pay === '0'"
              >未付款</el-tag
            >
            <el-tag type="success" v-else>已付款</el-tag>
          </template>
        </el-table-column>

        <el-table-column prop="order_pay" label="支付状态">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.order_pay === '1'"
              >支付宝</el-tag
            >
            <el-tag type="success" v-else-if="scope.row.order_pay === '2'"
              >微信</el-tag
            >
            <el-tag type="success" v-else-if="scope.row.order_pay === '3'"
              >银行卡</el-tag
            >
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>

        <el-table-column prop="is_send" label="是否发货"> </el-table-column>
        <el-table-column prop="update_time" label="下单时间">
          <template slot-scope="scope">
            {{ scope.row.update_time | dateFormat }}
          </template>
        </el-table-column>

        <el-table-column prop="" label="操作" width="180">
          <!-- 修改订单信息 -->
          <template v-slot="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              size="mini"
              @click="goEditPage(scope.row.order_id)"
            ></el-button>
            <!-- 查看物流 -->
            <el-button
              type="primary"
              round
              size="mini"
              @click="progress(scope.row.order_id)"
              >查看物流</el-button
            >
            <!-- 修改地址 -->
            <el-button
              type="success"
              icon="el-icon-map-location"
              circle
              size="mini"
              @click="showBox"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 修改地址对话框 -->
      <el-dialog
        title="修改地址"
        :visible.sync="addressVisible"
        width="50%"
        @close="addressDialogClosed"
      >
        <el-form
          :model="addressForm"
          :rules="addressFormRules"
          ref="addressFormRef"
          label-width="100px"
        >
          <el-form-item label="省市区/县" prop="address1">
            <el-cascader
              :options="cityData"
              v-model="addressForm.address1"
            ></el-cascader>
          </el-form-item>
          <el-form-item label="详细地址" prop="address2">
            <el-input v-model="addressForm.address2"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addressVisible = false">取 消</el-button>
          <el-button type="primary" @click="addressVisible = false"
            >确 定</el-button
          >
        </span>
      </el-dialog>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      pay_status: '',
      input3: '',
      tableData: [],
      value: '',
      timerId: null,
      pagenum: 1,
      pagesize: 5,
      total: 0,
      msg: '',
      // aaa:'',
      // color:'danger',
      addressVisible: false,
      addressForm: {
        address1: [],
        address2: '',
      },

      addressFormRules: {
        address1: [
          { required: true, message: '请选择省市区/县', trigger: 'blur' },
        ],
        address2: [
          { required: true, message: '请填写详细地址', trigger: 'blur' },
        ],
      },
      cityData: [
        {
          value: '北京市',
          label: '北京市',
          children: [
            {
              value: '海定区',
              label: '海定区',
              children: [
                {
                  value: '西三旗建材城西路',
                  label: '西三旗建材城西路',
                },
                {
                  value: '苏州街',
                  label: '苏州街',
                },
              ],
            },
            {
              value: '朝阳区',
              label: '朝阳区',
            },
          ],
        },
      ],
    }
  },
  created() {
    this.getListFn()
  },
  methods: {
    goEditPage(id) {
      this.$router.push('/orderEdit')
    },
    progress(id) {
      this.$router.push('/orderProgress')
    },
    // 修改地址对话框
    showBox() {
      this.addressVisible = true
    },
    addressDialogClosed() {
      this.$refs.addressFormRef.resetFields()
    },

    handleSizeChange(val) {
      this.pagesize = val
      this.getListFn()
    },
    handleCurrentChange(val) {
      this.pagenum = val
      this.getListFn()
    },
    // 获取数据
    async getListFn() {
      const res = await this.$http({
        method: 'get',
        url: 'orders',
        params: {
          pagenum: this.pagenum, //当前页码
          pagesize: this.pagesize, //每条显示条数
        },
      })
      this.tableData = res.data.data.goods
      this.total = res.data.data.total

      this.tableData.forEach((item, index) => {
        item.iser_id = index + 1
        if (item.order_pay === '1') {
          this.aaa = '支付宝'
          this.color = 'success'
        } else if (item.order_pay === '2') {
          this.aaa = '微信'
          this.color = 'success'
        } else if (item.order_pay === '3') {
          this.aaa = '银行卡'
          this.color = 'success'
        } else {
          this.aaa = '未付款'
          this.color = 'danger'
        }
      })
    },

    //  查看订单
    btnFn() {
      if (this.value === '') {
        // 没有必要调接口
        // 把搜索列表清空
        this.getListFn()
        return
      }
      this.tableData = this.tableData.filter((item) => {
        return item.order_number.includes(this.value)
      })
    },
  },
}
</script>

<style lang="less" scoped>
.el-input {
  width: 100%;
}

.input-with-select .el-input-group__prepend {
  background-color: #fff;
}

.el-cascader {
  width: 100%;
}
</style>
